import 'dart:io';

import 'package:bloctest/utils/http/BaseResponse.dart';
import 'package:dio/dio.dart';
import 'package:oktoast/oktoast.dart';

class ResInterceptor extends Interceptor {
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    BaseResponse res = BaseResponse.fromJson(response.data);
    if (response.statusCode == 200) {
      if (res.errorCode == 0) {
        if (res.data == null) {
          handler.next(
              Response(requestOptions: response.requestOptions, data: true));
        } else {
          handler.next(Response(
              requestOptions: response.requestOptions, data: res.data));
        }
      } else {
        showToast(res.errorMsg.toString());
        handler.reject(DioException(requestOptions: response.requestOptions));
      }
    } else if (res.errorCode == -1001) {
      handler.reject(DioException(
          requestOptions: response.requestOptions, message: "未登陆"));
    } else {
      showToast(res.errorMsg.toString());
      handler.reject(DioException(requestOptions: response.requestOptions));
    }
  }
}
